package com.hboxs.asl.controller.app.index;

import com.hboxs.asl.*;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.qcloudLVB.entity.Live;
import com.hboxs.asl.qcloudLVB.service.LiveService;
import com.hboxs.asl.service.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xxx on 2016/11/25
 */
@Controller("appRootController")
@RequestMapping("/app/root")
public class RootController extends BaseController {

    protected Logger logger = Logger.getLogger(getClass().getName());

    @Resource(name = "adServiceImpl")
    private AdService adService;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "liveServiceImpl")
    private LiveService liveService;

    /**
     * 首页
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public
    @ResponseBody
    Object index(Pageable pageable) {
        try {
            List<Ad> ads = adService.findListByAdPosition(Constant.WECHAT_AD_POSITION_MIDDLE);

            pageable.setPageSize(4);
            //获取易问易答
            List<Question> questions = questionService.findPageByAnswer(pageable).getContent();
            //获取分类
            List<Category> categories = categoryService.findAll();
            //获取直播中的课程
            List<Live> liveList = liveService.findPage(pageable, Live.LiveStatus.live, Live.Type.host).getContent();
            if (liveList.size() < 5) {
                List<Live> lives = liveService.findPage(pageable, Live.LiveStatus.finished, Live.Type.host).getContent();
                liveList.addAll(lives);
            }
            List<Course> courseList = new ArrayList<>();
            for (Live live : liveList) {
                if (live.getCourse()!= null) {
                    courseList.add(live.getCourse());
                }
            }

            //获取热门搜索
            pageable.setPageSize(10);
            pageable.getFilters().add(Filter.ne("courseType", Course.CourseType.nolive));
            List<Course> hotCourse = courseService.findHotSearchPage(pageable, true).getContent();
            List<String> hotTitle = new ArrayList<>();
            if (hotCourse.size() > 0) {
                for (Course course : hotCourse) {
                    hotTitle.add(course.getTitle());
                }
            }

            return JsonMessage.success(

                    "hotTitle", hotTitle,

                    "ads", JsonMessage.optimiseObj(ads, "id", "path", "url", "teacherId")

                    ,"questions", JsonMessage.optimiseObj(questions, "id", "title", "createDateF", "content", "mediaURL", "images", "teacher.avator", "teacher.name", "teacher.id", "timeLength",
                            "member.username", "name", "member.name", "payPrice", "payType", "birthDay", "birthHour", "gender", "email"
                            )

                    ,"categories", JsonMessage.optimiseObj(categories, "id", "name", "imageURL")

                    ,"courses", JsonMessage.optimiseObj(courseList, "password", "encrypt", "charge", "createDateF", "id", "imageURL", "introduction", "member.avator", "member.name", "price", "pubdate", "title", "applyCount", "live.watchCount", "live.videoURL", "live.hisWatchCount", "courseType")

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
